// pages/map/index.js
const dayjs = require('dayjs')
import { queryOrder, getOrderWorkOnDeliverySituation } from './../../api/order'

Page({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    timeStart: dayjs().format('YYYY-MM-DD 00:00:00'),
    timeEnd: dayjs().add(1, 'day').format('YYYY-MM-DD 00:00:00'),
    status: '',
    latitude: 23.099994,
    longitude: 113.324520,
    markers: [],
    statusList: [{
      title: '全部',
      value: '',
    },{
      title: '待配送',
      value: '4, 5, 6',
    },{
      title: '配送中',
      value: '7',
    }],
    statusTitleList: ['全部', '待配送', '配送中'],
    showMap: true,
    orderList: [],
  },

  // 修改日期
  handledDateRange(event) {
    const {start, end} = event.detail
    this.setData({
      timeStart: start,
      timeEnd: end
    })
    console.log(start, end)
  },
  handleStatusChange(event) {
    const {value} = event.detail
    this.setData({
      status: value
    })
  },
  // 加载数据
  loadData() {
    const {timeStart, timeEnd, status} = this.data
    const params = {
      pageNum: 1,
      pageSize: 15,
      ordStates: status,
      delyStartTime: timeStart,
      delyEndTimeEnd: timeEnd,
    }
    queryOrder(params).then(res => {
      console.log(res)
      const list = res.data.list || []
      const latitude = list[0] && list[0].addressLat
      const longitude = list[0] && list[0].addressLng
      this.setData({
        orderList: list,
        markers: list.map((item, index) => {
          return {
            orginData: item,
            id: index,
            longitude: item.addressLng,
            latitude: item.addressLat,
            title: item.address1,
            width: 28,
            height: 40,
            callout: {
              content: `${item.xmbzNo}\n${item.address1}\n${item.delySTime}-${item.delyETime}`,
              display: 'ALWAYS',
              bgColor: "#57A9F7",
              padding: "5px",
              borderRadius: "2px",
              borderWidth: "1px",
              borderColor: "#57A9F7",
              color: '#fff',
              textAlign: 'center'
            }
          }
        }),
        latitude: latitude || 23.099994,
        longitude: longitude || 113.324520,
      })
    })
  },

  /**
   * 点击点获取相关信息
   */ 
  makertap: function(e) {
    const marker = this.data.markers.find(item => e.markerId === item.id)
    if (marker) {
      wx.navigateTo({
        url: `../detail/detail?proid=${marker.orginData.ordNo}`
      })
    }
  },
  
  // 切换地图
  triggerMap() {
    this.setData({
      showMap: !this.data.showMap
    })
  },

  // 跳转详情页
  toDetail: function (e) {
    console.log(e.currentTarget.dataset.item.ordNo,e.currentTarget.dataset)
    const ordNo = e.currentTarget.dataset.item.ordNo
    wx.navigateTo({
      url: `../detail/detail?proid=${ordNo}`
    })
  },

  // 地图定位
  location: function (e) {
    const data = e.detail
    console.log(data)
    this.setData({
      showMap: true,
      longitude: data.addressLng,
      latitude: data.addressLat,
    })
  },

  onLoad() {
    const {timeStart, timeEnd} = this.data
    const dateRangeCom = this.selectComponent('#dateRange')
    dateRangeCom.setDefaultDate([timeStart, timeEnd])
    this.loadData()
  },
})